/*
 一些公用的JS方法
 */

/**
 * 格式化数字为千分位,分割的样式
 * @param val 需要格式化的值
 * @param isDecimal  true|false 是否将整数后面补上两位小数
 * @returns {*}
 */
function formatNumber(val, isDecimal) {
    if (val != null && !isNaN(val)) {
        var b = val.toString();
        var num = "";
        var num2 = "";
        var result = "";
        var decimal = false;
        if (b.indexOf(".") > 0) {
            var arr = b.split('.');
            num = arr[0].toString();
            num2 = arr[1].toString();
            decimal = true;
        }else{
            num = b;
        }

        while (num.length > 3) {
            result = ',' + num.slice(-3) + result;
            num = num.slice(0, num.length - 3);
        }
        if (num) {
            result = num + result;
        }

        if (decimal) {
            return result + "." + num2;
        }

        if (isDecimal) {
            return result + ".00";
        } else {
            return result;
        }
    }
    return "";
}


function mySelfloadFilter(data) {
    var result = {
        status: data.status,
        msg: data.msg,
        PG: data.data
    };

    var value = {
        total: result.PG["rows"]["total"],
        rows: result.PG["rows"]["rows"]
    };

    return value;
}


function formtojson(form) {
    var tojson = "{";
    $.each(form, function (i, item) {
        tojson += "\"" + item.name + "\":\"" + item.value + "\"";
        if (form.length - 1 != i) {
            tojson += ',';
        }
    });
    tojson += "}";
    return tojson;
}


//时间
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds()
        // millisecond
    }
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

function formatDatebox(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    } else {

        dt = new Date(value);

    }

    return dt.format("yyyy-MM-dd hh:mm:ss"); //扩展的Date的format方法(上述插件实现)
}



function GetArgsFromHref(sHref, sArgName) {
    var args = sHref.split("?");
    var retval = "";
    if (args[0] == sHref) {
        return retval;
    }
    var str = args[1];
    args = str.split("&");
    for (var i = 0; i < args.length; i++) {
        str = args[i];
        var arg = str.split("=");
        if (arg.length <= 1) continue;
        if (arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}

//审核状态转文字,统一维护
function StateCodetoString(val, rec) {
    if (val == '0') {
        return '早班'
    } else if (val == '1') {
        return '中班'
    }else if (val == '2') {
        return '晚班'
    }
}

//投资状态，目前就已投和拟投
function investmentStateCodetoString(val) {
    if (val == true) {
        return '已投'
    } else if (val == false) {
        return '拟投'
    }else if(val == 'true'){
        return '已投'
    }else if(val == 'false'){
        return '拟投'
    }

}







function formatState(val) {
    if (val == true) {
        return '启用'
    } else if (val == false) {
        return '废弃'
    }else if(val == 'true'){
        return '启用'
    }else if(val == 'false'){
        return '废弃'
    }
    return val
}

//是否退出
function isOut(val) {
    if (val == true) {
        return '已退'
    } else if (val == false) {
        return '未退'
    }
}
//取小参数
function GetQueryString(arg) {
    var reg = new RegExp("(^|&)" + arg + "=([^&]*)(&|$)","i");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return (r[2]); return null;
};